<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Form\Login;
use Admin\Form\LoginValidator;
use Admin\Model\Login as LoginService;

class LoginController extends AbstractActionController{
    private $login;
    public function setLogin(LoginService $login){
        $this->login=$login;
    }
    public function autenticarAction(){
        if(!$this->request->isPost()){
            $this->redirect()->toRoute('admin',array('controller' => 'login'));
        }
        $form= new Login("login");
        $form->setInputFilter(new LoginValidator());
        
        $data=$this->request->getPost();
        
        $form->setData($data);
        
        if(!$form->isValid()){
            $modelView= new ViewModel(array('form'=>$form));
            $modelView->setTemplate('admin/login/index');
            return $modelView;
        }
        $values=$form->getData();
        $usuario=$values['usuario'];
        $pass=$values['pass'];
        try{
            $this->login->setMessage('El nombre de Usuario y Password no coinciden.',
                    LoginService::NOT_IDENTITY);
            $this->login->setMessage('La contraseña ingresada es incorrecta. Inténtelo de nuevo.',
                    LoginService::INVALID_CREDENTIAL);
            $this->login->setMessage('Los campos de Usuario y Password no pueden dejarse en blanco.',
                    LoginService::INVALID_LOGIN);
            $this->login->login($email,$clave);
            
            $this->layout()->mensaje="Login Correcto";
            $this->layout()->colorMensaje="green";
            return $this_>forward()->dispatch('Admin\Controller\Login',
                    array('action'=>'success'));
            
            
        } catch(Exception $e){
            $this->layout()->mensaje=$e->getMessage();
            return $this->forward()->dispatch('Admin\Controller\Login',
                    array('action'=>'index'));
        }
    }
}
?>
